package com.hjj.controller;

import com.hjj.beans.ActivitiesRemark;
import com.hjj.beans.User;
import com.hjj.dto.ResultDTO;
import com.hjj.exception.CRUDException;
import com.hjj.exception.ParameterValidException;
import com.hjj.services.ActivitiesRemarkServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.Locale;


@RestController
@RequestMapping("/ActivitiesRemark")
public class ActivitiesRemarkController {
    @Autowired
    ActivitiesRemarkServices activitiesRemarkServices;

    @Autowired
    MessageSource messageSource;

    @RequestMapping("add.action")
    public ResultDTO add(@Valid ActivitiesRemark activitiesRemark, BindingResult bindingResult, HttpSession httpSession) throws CRUDException, ParameterValidException {

        if (bindingResult.hasErrors()){
            // 获取消息资源的键
            String message = bindingResult.getFieldError().getDefaultMessage();

            // 根据键获取验证消息的值
            throw new ParameterValidException( messageSource.getMessage(message, null, Locale.getDefault()));
        }

        // 获取当前登录者名称并添加到备注信息中
        activitiesRemark.setNotePerson(((User)httpSession.getAttribute("user")).getName());

        boolean add = activitiesRemarkServices.add(activitiesRemark);

        if (!add) throw new CRUDException("备注信息添加失败");

        ResultDTO resultDTO = new ResultDTO();
        resultDTO.setResult(ResultDTO.SUCCESS);

        return resultDTO;
    }

    @RequestMapping("edit.action")
    public ResultDTO edit(@Valid ActivitiesRemark activitiesRemark,BindingResult bindingResult, HttpSession httpSession) throws CRUDException, ParameterValidException {
        if (bindingResult.hasErrors()){
            // 获取消息资源的键
            String message = bindingResult.getFieldError().getDefaultMessage();

            // 根据键获取验证消息的值
            throw new ParameterValidException( messageSource.getMessage(message, null, Locale.getDefault()));
        }

        // 获取当前登录的用户名
        String userName = ((User) httpSession.getAttribute("user")).getName();

        ActivitiesRemark temp = activitiesRemarkServices.get(activitiesRemark.getId());

        if (!(temp.getNotePerson().equals(userName))) throw new CRUDException("当前备注不是你创建的，你没有修改权利");

        // 给备注信息添加修改者信息
        activitiesRemark.setEditPerson(userName);

        boolean edit = activitiesRemarkServices.edit(activitiesRemark);

        if (!edit) throw new CRUDException("备注信息修改失败");

        ResultDTO resultDTO = new ResultDTO();
        resultDTO.setResult(ResultDTO.SUCCESS);
        return resultDTO;
    }


    @RequestMapping("del.action")
    public ResultDTO del(String id, HttpSession httpSession) throws CRUDException {
        // 获取当前登录的用户名
        String userName = ((User) httpSession.getAttribute("user")).getName();

        ActivitiesRemark temp = activitiesRemarkServices.get(id);

        if (!(temp.getNotePerson().equals(userName))) throw new CRUDException("当前备注不是你创建的，你没有删除权利");

        boolean del = activitiesRemarkServices.del(id);

        if (!del) throw new CRUDException("备注信息删除失败");

        ResultDTO resultDTO = new ResultDTO();
        resultDTO.setResult(ResultDTO.SUCCESS);
        return resultDTO;
    }
}
